import $ from '@/common'
var Base64 = require('js-base64').Base64

const Render = {
  tag: {
    // 身份证号，显示生日日期
    cert_code: {
      render({node, text}) {
        text = text || ''
        var year = text.substring(6, 10)
        var date = text.substring(10, 14)
        if (text.length && year.length) {
          text = text.replace(year, `<a>${year}</a>`)
          text = text.replace(date, `<span class="light">${date}</span>`)
          node.innerHTML = text
        }
      }
    },
    cert: {
      render({node, text}) {
        var code = text
        if (!node.getAttribute('noencode')) {
          code = Base64.decode(code)
        }
        try {
          node.innerHTML = $.coverValue(code, node.getAttribute('type'))
        } catch (ex) {
          node.innerHTML = text
        }
      }
    },
    badge_case_process: {
      render({node, text, field, row, value}) {
        var className = 'badge badge-case_process'
        if (text === '新') {
          className += ' badge-case_process-new'
        }
        if (text && text !== '' && text !== ' ') {
          node.innerHTML = `<span class="${className}">${text}</span>`
        }
      }
    },
    Audio_player: {
      render({node, text, field, row, value}) {
        var className = 'ivu-btn ivu-btn-success btn-audio'
        var src = node.attributes['src'] && node.attributes['src'].value
        var duration = node.getAttribute('length')
        if (duration) {
          duration = ' ' + duration
        } else {
          duration = ''
        }
        var time = duration
        duration = parseInt(duration) || 0
        if (duration > 0) {
          let m = Math.floor(duration / 60)
          let s = duration - m * 60
          if (s < 10) {
            s = '0' + s
          }
          time = m + ':' + s
        }
        if (time <= 0) time = ''
        if (src) {
          src = $.composeValue(src, row)
        } else {
          src = value
        }
        // 通过全局绑定的事件来进行音频播放
        if (src && src.length > 1 && $.trim(src).length > 1) {
          node.innerHTML = `
        <button class="${className}" data-audio="${src}" data-length="${duration}">
          <i class="ivu-load-loop ivu-icon ivu-icon-load-c"> </i>
          <span class="ivu-icon ivu-icon-play player-icon"> </span>
          <span class="ivu-icon ivu-icon-pause player-icon"> </span>
          <span class="player-text"> 播放 ${time}</span>
          <div class="player-mask"></div>
        </button>`
        }
      }
    }
  },
  tpl(str, field, row) {
    str = `<div>${str}</div>`
    var doc = new DOMParser().parseFromString(str, 'text/xml').children[0]
    $.eachChild({
      data: doc,
      handler: (node) => {
        if (Render.tag[node.nodeName]) {
          Render.tag[node.nodeName].render({
            node,
            text: node.innerHTML,
            field,
            row,
            value: row[field.code]
          })
        }
      }
    })
    return doc.innerHTML
  }
}
export default Render
